<template>
  <el-dialog
    title="全局设置"
    :visible="true"
    width="700px"
    @close="$emit('close')"
  >
    <el-form :model="settings" label-width="120px">
      <el-form-item label="主题">
        <el-radio-group v-model="settings.theme">
          <el-radio label="light">浅色</el-radio>
          <el-radio label="dark">深色</el-radio>
          <el-radio label="auto">跟随系统</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="语言">
        <el-select v-model="settings.language">
          <el-option label="简体中文" value="zh-CN" />
          <el-option label="English" value="en-US" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="自动保存">
        <el-switch v-model="settings.autoSave" />
        <span class="form-tip">自动保存请求历史和配置</span>
      </el-form-item>
      
      <el-form-item label="请求超时">
        <el-input-number
          v-model="settings.requestTimeout"
          :min="1000"
          :max="300000"
          :step="1000"
        />
        <span class="form-tip">毫秒</span>
      </el-form-item>
      
      <el-form-item label="历史记录上限">
        <el-input-number
          v-model="settings.maxHistoryItems"
          :min="100"
          :max="10000"
          :step="100"
        />
        <span class="form-tip">条</span>
      </el-form-item>
      
      <el-form-item label="代理设置">
        <el-input
          v-model="settings.proxy"
          placeholder="例如: http://127.0.0.1:8080"
        />
      </el-form-item>
      
      <el-form-item label="证书验证">
        <el-switch v-model="settings.verifyCertificate" />
        <span class="form-tip">HTTPS请求时验证SSL证书</span>
      </el-form-item>
      
      <el-form-item label="跟随重定向">
        <el-switch v-model="settings.followRedirect" />
        <span class="form-tip">自动跟随3xx重定向响应</span>
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="resetSettings">重置</el-button>
      <el-button type="primary" @click="saveSettings">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  name: 'SettingsDialog',
  data() {
    return {
      settings: {
        theme: 'light',
        language: 'zh-CN',
        autoSave: true,
        requestTimeout: 30000,
        maxHistoryItems: 1000,
        proxy: '',
        verifyCertificate: true,
        followRedirect: true
      }
    }
  },
  computed: {
    ...mapState(['globalSettings'])
  },
  created() {
    this.settings = { ...this.globalSettings }
  },
  methods: {
    ...mapActions(['updateGlobalSettings']),
    
    saveSettings() {
      this.updateGlobalSettings(this.settings)
      this.$message.success('设置已保存')
      this.$emit('close')
    },
    
    resetSettings() {
      this.settings = {
        theme: 'light',
        language: 'zh-CN',
        autoSave: true,
        requestTimeout: 30000,
        maxHistoryItems: 1000,
        proxy: '',
        verifyCertificate: true,
        followRedirect: true
      }
      this.$message.success('设置已重置')
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../styles/variables.scss';

.form-tip {
  margin-left: $spacing-base;
  color: $text-secondary;
  font-size: $font-size-small;
}
</style>